#ifndef THUNDERRAID_PLANE_H
#define THUNDERRAID_PLANE_H

#include "gameitem.h"

#include <QPixmap>
#include <QMap>
#include <QSoundEffect>

struct BulletInfo{
    QString name;
    QPixmap pixmap;
    int damage;
    qreal speed;
    qreal interval;
};

class Bullet : public GamePixmapItem{
    Q_OBJECT
public:
    static QList<BulletInfo> heroAvailableList;
    static QList<BulletInfo> enemyAvailableList;
    explicit Bullet(const QPixmap &pixmap, int damage, qreal speed, GameView *view = nullptr);
    explicit Bullet(const BulletInfo &info);

    [[nodiscard]] int damage() const;

private:
    int _damage;
};

struct PlaneInfo{
    QPixmap pixmap;
    int hp;
    qreal speed;
    QString bullet_name;
};

class Plane : public GamePixmapItem{
    Q_OBJECT
public:

    explicit Plane(const QPixmap &pixmap, GameView *view = nullptr);
    explicit Plane(const PlaneInfo &info, GameView *view = nullptr);

    void changeHp(int diff_amount);
    void damage(int amount);
    void heal(int amount);
    void update(qreal interval_ms) override;
    QList<Bullet *> GenerateBullet();
    void setBullet(BulletInfo new_bulletInfo);
    void setCount(uint8_t new_count);
    [[nodiscard]] int getHp() const;
    [[nodiscard]] qreal getMaxSpeed() const;
    void setBulletCount(int bulletCount);
    int getBulletCount() const;
    const BulletInfo &getBulletInfo() const;
signals:
   void hpChanged(int new_value, int diff);
   void planeDestroy(Plane *);

protected:
    virtual void shoot() = 0;
    virtual qreal bulletX();
    virtual qreal bulletY();

private:
    int hp;
    qreal max_speed;
    BulletInfo bulletInfo;
    int bullet_count = 1;

    qreal shoot_interval_count = 0;
};

class HeroPlane: public Plane{
public:
    explicit HeroPlane(const PlaneInfo &info, GameView *view = nullptr);

protected:
    void shoot() override;

};

class EnemyPlane: public Plane{
public:
    explicit EnemyPlane(const PlaneInfo &info, GameView *view = nullptr);

protected:
    void shoot() override;
    qreal bulletY() override;
};

class Bomb: public GamePixmapMovieItem{
    Q_OBJECT
public:
    explicit Bomb(GameView *view = nullptr);
    void start() override;
private:
    QSoundEffect _explosion;
};

class PlaneFactor{
public:
    static HeroPlane getHeroPlane(const QString &name);
    static EnemyPlane getEnemyPlane(const QString &name);

    static void addHeroPlane(const QString &name, const PlaneInfo& info);
    static void removeHeroPlane(const QString &name);
    static void addEnemyPlane(const QString &name,const PlaneInfo& info);
    static void removeEnemyPlane(const QString &name);

private:
    inline static QMap<QString, PlaneInfo> hero, enemy;
};

class BulletFactor{
public:
    static BulletInfo getHeroBullet(const QString &name);
    static BulletInfo getEnemyBullet(const QString &name);

    static void addHeroBullet(const QString &name,const BulletInfo& info);
    static void removeHeroBullet(const QString &name);
    static void addEnemyBullet(const QString &name,const BulletInfo& info);
    static void removeEnemyBullet(const QString &name);

private:
    inline static QMap<QString, BulletInfo> hero, enemy;
};


#endif//THUNDERRAID_PLANE_H
